<template>
  <div>
    <el-dialog :title="formData.id ?'编辑页面':'新增页面'" :visible="showDialog" width="60%" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content" style="height: 200px;">
          <!-- <el-input v-model="formData.content" type="textarea" resize="none" /> -->
          <quill-editor v-model="formData.content" @blur="$refs.form.validateField('content')" />
        </el-form-item>
      </el-form>

      <template v-slot:footer>
        <el-row type="flex" justify="end">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button size="small" type="primary" @click="submit">确认</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addPages, editPages, editPagesId } from '@/api/pages'

export default {

  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        content: ''

      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    // 获取指定分类
    async editPagesId(id) {
      const res = await editPagesId(id)
      console.log(56, res)
      this.formData = res.data
    },
    closeFn() {
      this.$emit('closeEvent')
      //   使用语法：
      // 子组件：this.$emit('update:要修改的props属性名',更新的值)
      // 父组件
      // <AddRole 属性名.sync = '父组件中的变量'></AddRole>
      this.$emit('update:showDialog', false)
      // 清空数据
      this.formData = {
        id: '',
        title: '',
        content: ''

      }

      // 清空效验
      this.$refs.form.resetFields()
    },
    submit() {
    //   await addPages(this.formData)

      this.$refs.form.validate(async bool => {
        if (bool) {
          // 表单验证
          if (this.formData.id) {
            await editPages(this.formData)
          } else {
            // 效验通过，调用接口
            await addPages(this.formData)

            // console.log(123)
          }

          this.closeFn()
          //   更新列表
          this.$emit('updateEvent')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.ql-container.ql-snow{
  height: 100px;
}
</style>
